<?php

namespace app\logic\model\music;

use app\api\model\music\MusicSelectedPackDetailApiModel;
use app\logic\model\LogicModel;
use app\logic\model\user\UserLogicModel;
use think\model\concern\SoftDelete;

class MusicSelectedPackLogicModel extends LogicModel
{
    use SoftDelete;

    protected $name='music_selected_pack';

    protected $deleteTime = 'delete_time';
    public function getList(\think\Request $request, array $where, string $field='*', $page = 1, $limit=15,$order='id desc')
    {
        $query=$this->where($where)
            ->where(function ($q) use ($request){
                if(check_request($request,'title')){
                    $q->whereLike("title","%{$request['title']}%");
                }
                if(check_request($request,'type')){
                    $q->where("find_in_set({$request['type']},type_id)");
                }
                if(check_request($request,'ids')){
                    $q->whereIn("ids",$request['ids']);
                }
                if(check_request($request,'start_time')&&check_request($request,'end_time')){
                    $q->whereBetween("create_time",[$request['start_time'],$request['end_time']." 23:59:59"]);
                }elseif (check_request($request,'start_time')){
                    $q->where("create_time",">=",$request['start_time']);
                }elseif (check_request($request,'end_time')){
                    $q->where("create_time","<=",$request['end_time']." 23:59:59");
                }
            });
        $list = $query
            ->field($field)
            ->order($order)
            ->page($page,$limit)
            ->select();
        $count = $query->count();
        if($count >= 1){
            foreach ($list as &$val){
                $val['type'] = $this->getType($val);
            }
            unset($val);
        }
        return [$list,$count];
    }

    function getType($info,$info_id=0){
        if(empty($info)){
           $info = $this->getFindById($info_id);
        }
        return MusicSelectedPackTypeLogicModel::whereIn('id',$info['type_id'])
            ->field('id,title')->select();
    }
}